{js:artTemplate}
<div class="wrapper clearfix">
	<div class="position mt_10"> <span>您当前的位置：</span> <a href="{url:}"> 首页</a> » 购物车</div>
	<div class="myshopping m_10">
		<ul class="order_step">
			<li class="current"><span class="first">1、查看购物车</span></li>
			<li><span>2、填写核对订单信息</span></li>
			<li class="last"><span>3、成功提交订单</span></li>
		</ul>
	</div>

	<div id="cart_prompt" class="cart_prompt f14 t_l" style="display:none">
		<p class="m_10 gray"><b class="orange">恭喜，</b>您的订单已经满足了以下优惠活动！</p>
	</div>

	<!--促销规则模板-->
	<script type="text/html" id="promotionTemplate">
		<p class="indent blue"><%=item['plan']%>，<%=item['info']%></p>
	</script>

	<table width="100%" class="cart_table m_10">
		<colgroup>
			<col width="115px" />
			<col />
			<col width="80px" />
			<col width="80px" />
			<col width="80px" />
			<col width="80px" />
			<col width="80px" />
			<col width="80px" />
		</colgroup>

		<caption>查看购物车</caption>
		<thead>
			<tr><th>图片</th><th>商品名称</th><th>赠送积分</th><th>单价</th><th>优惠</th><th>数量</th><th>小计</th><th class="last">操作</th></tr>
		</thead>
		<tbody>
			{foreach:items = $this->goodsList}
			<tr>
				<td><img src="{url:/pic/thumb/img/$item[img]/w/66/h/66}" width="66px" height="66px" alt="{$item['name']}" title="{$item['name']}" /></td>
				<td class="t_l">
					<a href="{url:/site/products/id/$item[goods_id]}" class="blue">{$item['name']}</a>
					{if:isset($item['spec_array'])}
					<p>
					{set:$spec_array=Block::show_spec($item['spec_array']);}
					{foreach:items=$spec_array item=$specValue key=$specName}
						{$specName}：{$specValue} &nbsp&nbsp
					{/foreach}
					</p>
					{/if}
				</td>
				<td>{$item['point']}</td>
				<td><b>￥{$item['sell_price']}</b></td>
				<td>减￥{$item['reduce']}</td>
				<td>
					<div class="num">
						{set:$item_json = JSON::encode($item)}
						<a class="reduce" href="javascript:void(0)" onclick='cart_reduce({$item_json});'>-</a>
						<input class="tiny" value="{$item['count']}" onchange='cartCount({$item_json});' type="text" id="count_{$item['goods_id']}_{$item['product_id']}" />
						<a class="add" href="javascript:void(0)" onclick='cart_increase({$item_json});'>+</a>
					</div>
				</td>
				<td>￥<b class="red2" id="sum_{$item['goods_id']}_{$item['product_id']}">{$item['sum']}</b></td>
				<td><a href='javascript:removeCartByJSON({$item_json});'>删除</a></td>
			</tr>
			{/foreach}

			<tr class="stats">
				<td colspan="8">
					<span>商品总重量：<b id='weight'>{$this->weight}</b>g</span><span>商品总金额：￥<b id='origin_price'>{$this->sum}</b> - 商品优惠：￥<b id='discount_price'>{$this->reduce}</b> - 促销活动优惠：￥<b id='promotion_price'>{$this->proReduce}</b></span><br />
					金额总计（不含运费）：￥<b class="orange" id='sum_price'>{$this->final_sum}</b>
				</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2" class="t_l">
					<a class="del" href="javascript:void(0);" onclick="delModel({msg:'确定要清空购物车么？',link:'{url:/simple/clearCart}'});">清空购物车</a>
				</td>
				<td colspan="6" class="t_r">
					<a class="btn_continue" href="javascript:void(0)" onclick="window.history.go(-1);">继续购物</a>
					{if:$this->goodsList}
					<a class="btn_pay" href="{url:/simple/cart2}">去结算</a>
					{/if}
				</td>
			</tr>
		</tfoot>
	</table>
</div>

<script type='text/javascript'>
jQuery(function()
{
	{if:$this->promotion}
	{foreach:items = $this->promotion}
	$('#cart_prompt').append( template.render('promotionTemplate',{"item":{echo:JSON::encode($item)}}) );
	{/foreach}
	$('#cart_prompt').show();
	{/if}
});
//购物车数量改动计算
function cartCount(obj)
{
	var countInput = $('#count_'+obj.goods_id+'_'+obj.product_id);
	var countInputVal = parseInt(countInput.val());
	var oldNum = countInput.data('oldNum') ? countInput.data('oldNum') : obj.count;

	//商品数量大于1件
	if(isNaN(countInputVal) || (countInputVal <= 0))
	{
		alert('购买的数量必须大于1件');
		countInput.val(1);
		countInput.change();
	}
	//商品数量小于库存量
	else if(countInputVal > parseInt(obj.store_nums))
	{
		alert('购买的数量不能大于此商品的库存量');
		countInput.val(parseInt(obj.store_nums));
		countInput.change();
	}
	else
	{
		var diff = parseInt(countInputVal) - parseInt(oldNum);
		if(diff == 0)
		{
			return;
		}

		var goods_id   = obj.product_id > 0 ? obj.product_id : obj.goods_id;
		var goods_type = obj.product_id > 0 ? "product"      : "goods";

		//更新购物车中此商品的数量
		$.getJSON("{url:/simple/joinCart}",{"goods_id":goods_id,"type":goods_type,"goods_num":diff,"random":Math.random()},function(content){
			if(content.isError == true)
			{
				alert(content.message);
				countInput.val(1);
				countInput.change();
			}
			else
			{
				var goodsId   = [];
				var productId = [];
				var num       = [];
				$('[id^="count_"]').each(function(i)
				{
					var idValue = $(this).attr('id');
					var dataArray = idValue.split("_");

					goodsId.push(dataArray[1]);
					productId.push(dataArray[2]);
					num.push(this.value);
				});
				countInput.data('oldNum',countInputVal);
				$.getJSON("{url:/simple/promotionRuleAjax}",{"goodsId":goodsId,"productId":productId,"num":num,"random":Math.random()},function(content){
					if(content.promotion.length > 0)
					{
						$('#cart_prompt .indent').remove();

						for(var i = 0;i < content.promotion.length; i++)
						{
							$('#cart_prompt').append( template.render('promotionTemplate',{"item":content.promotion[i]}) );
						}
						$('#cart_prompt').show();
					}
					else
					{
						$('#cart_prompt .indent').remove();
						$('#cart_prompt').hide();
					}

					/*开始更新数据*/
					$('#weight').html(content.weight);
					$('#origin_price').html(content.sum);
					$('#discount_price').html(content.reduce);
					$('#promotion_price').html(content.proReduce);
					$('#sum_price').html(content.final_sum);
					$('#sum_'+obj.goods_id+'_'+obj.product_id).html((obj.sell_price * countInputVal).toFixed(2));
				});
			}
		});
	}
}

//增加商品数量
function cart_increase(obj)
{
	//库存超量检查
	var countInput = $('#count_'+obj.goods_id+'_'+obj.product_id);
	if(parseInt(countInput.val()) + 1 > parseInt(obj.store_nums))
	{
		alert('购买的数量大于此商品的库存量');
	}
	else
	{
		countInput.val(parseInt(countInput.val()) + 1);
		countInput.change();
	}
}

//减少商品数量
function cart_reduce(obj)
{
	//库存超量检查
	var countInput = $('#count_'+obj.goods_id+'_'+obj.product_id);
	if(parseInt(countInput.val()) - 1 <= 0)
	{
		alert('购买的数量必须大于1件');
	}
	else
	{
		countInput.val(parseInt(countInput.val()) - 1);
		countInput.change();
	}
}

//移除购物车
function removeCartByJSON(obj)
{
	var goods_id   = obj.product_id > 0 ? obj.product_id : obj.goods_id;
	var goods_type = obj.product_id > 0 ? "product"      : "goods";
	$.getJSON("{url:/simple/removeCart}",{"goods_id":goods_id,"type":goods_type,"random":Math.random()},function()
	{
		window.location.reload();
	});
}
</script>